package servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.stanford.nlp.ling.TaggedWord;

import triple.Triple;
import annotate.Annotate;

/**
 * 标注三元组servlet
 * @author Da Huang (dhuang.cn@gmail.com)
 *
 */
public class AnnotateTriple extends HttpServlet {

	private static final long serialVersionUID = 3244382811047878138L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		@SuppressWarnings("unchecked")
		List<Entry<String, String>> trunks = (List<Entry<String, String>>) session.getAttribute("trunks");
		for (int i = 0; i < trunks.size(); i ++) {
			String type = new String(req.getParameter("trunk-" + i).getBytes("ISO8859-1"), "UTF-8");
			trunks.get(i).setValue(type);
		}
		session.setAttribute("trunks", trunks);
		
		List<Entry<String, String>> newWords = Annotate.getInstance().getNewWords(trunks);
		session.setAttribute("newWords", newWords);

		@SuppressWarnings("unchecked")
		List<String> segWords = (List<String>) session.getAttribute("segWords");
		@SuppressWarnings("unchecked")
		List<TaggedWord> tagWords = (List<TaggedWord>) session.getAttribute("tagWords");
		List<Triple> triples = Annotate.getInstance().annotateTriple(segWords, trunks, tagWords);
		req.setAttribute("triples", triples);
		
		req.getRequestDispatcher("index4.jsp").forward(req, resp);
	}
}
